<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 佣金记录
                </el-breadcrumb-item>
            </el-breadcrumb>

        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.mobile"  placeholder="手机号"  class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="success"  style="float: right" icon="el-icon-upload2" @click="uploadVisible = true">上传佣金</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column prop="id" label="Id" width="55" align="center"></el-table-column>
                <el-table-column prop="mobile" label="手机号"></el-table-column>
                <el-table-column prop="nickname" label="昵称"></el-table-column>
<!--                <el-table-column prop="direct_td_achievement" label="直推业绩"></el-table-column>-->
                <el-table-column prop="amount" label="实际佣金"></el-table-column>
                <el-table-column prop="td_achievement" label="表格佣金"></el-table-column>
                <el-table-column prop="tag" label="日期"></el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="prev, next, pager"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>

        </div>

        <el-dialog
                title="上传代理佣金"
                stlye="width:35%"
                :visible.sync="uploadVisible">
            <el-upload
                    class="upload-demo"
                    ref="upload"
                    action="/admin/commission_upload"
                    :limit="1"
                    :headers="header"
                    :file-list="fileList"
                    :on-success="handleSuccess"
                    :auto-upload="false">
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button style="display: block;margin: 10px 0 0 125px"  size="small" type="success" @click="submitUpload">上传到服务器</el-button>
            </el-upload>
        </el-dialog>
        <el-dialog
                title="成功{{successCount}}条, 以下UUID未匹配到用户, 需先注册"
                stlye="width:35%"
                :visible.sync="errVisible">
            <div style="color: #f02d2d">
                {{errUser}}
            </div>
        </el-dialog>
    </div>
</template>

<script>

export default {
    name: 'order',
    data() {
        return {
            query: {
                page: 1,
                pageSize: 10,
            },
            tableData: [],
            pageTotal: 0,
            uploadVisible: false,
            fileList: [],
            errVisible: false,
            errUser: '',
            successCount: '',
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getData() {
            this.http.achievement(this.query).then(res => {
                if(res.code ===  0) {
                    this.tableData = res.data.data;
                    this.pageTotal = res.data.total;
                } else {
                    this.$message.error(res.msg);
                }
            });
        },
        submitUpload() {
            this.$message.success("上传中, 请稍后!");

            this.$refs.upload.submit();
        },
        handleSuccess(res, file){
            if(res.data.user.length === 0) {
                this.$message.success("全部上传更新成功" + res.data.success + "条");
            } else {
                this.errUser = res.data.user.join(' | ')
                this.successCount = res.data.success
                this.errVisible = true
            }
            this.fileList = []
            this.uploadVisible = false
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'page', 1);
            this.getData();
        },
        // 编辑操作
        confirmOk(index, row) {
            this.http.doWithdraw({id: row.id}).then(res => {
                this.getData();
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        }
    },
    computed: {
        header () {
            return {token: localStorage.getItem('user_token')}
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.el-upload--text{
    height: 100px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
